//============================================================================
// Name        : vector_example.cpp
// Author      : marioc
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

// vector_reserve.cpp

#include <vector>
#include <stdint.h>
#include <algorithm>
#include <functional>
#include <cstdlib>


#include "container_print.h"


using namespace std;
using namespace tips;

static void prepare_data(vector<int> &v1, uint16_t size);
static void prepare_random_data(vector<int> &v);

int prova(void);

int main( )
{
   // fill a vector with one million numbers the slow way
   vector<int> v1;
   vector<int> v2;
   vector<int> vrandom (8);
   vector<int> vr (10);

   for( int i = 0; i < 1000000; ++i )
      v1.push_back( i );


   prepare_data(v2, 100);

   prepare_random_data(vr);

   prova();

   cout << "end" << endl;
}


void
prepare_data(vector<int> &v, uint16_t size)
{
	v.reserve( size ); // still has zero elements
	for( int i = 0; i < size; ++i )
		v.push_back( i );

	tips::print( v, "linear data");
}




void prepare_random_data(vector<int> &random)
{
   // fill the container with random numbers
   generate( random.begin(), random.end(), rand );

   // make all random numbers go from 1 to 6
   transform( random.begin(), random.end(), random.begin(), bind2nd( modulus<int>(), 6 ) );
   transform( random.begin(), random.end(), random.begin(), bind2nd( plus<int>(), 1 ) );
   tips::print( random, "Random numbers from 1 to 6" );
}



// adjacent_difference example
#include <iostream>     // std::cout
#include <functional>   // std::multiplies
#include <numeric>      // std::adjacent_difference

int myop (int x, int y) {return x+y;}

int
prova (void)
{
  const int val[] = {1,2,3,5,9,11,12};
  int result[7];

  std::adjacent_difference (val, val+7, result);
  std::cout << "using default adjacent_difference: ";
  for (int i=0; i<7; i++) std::cout << result[i] << ' ';
  std::cout << '\n';

  std::adjacent_difference (val, val+7, result, std::multiplies<int>());
  std::cout << "using functional operation multiplies: ";
  for (int i=0; i<7; i++) std::cout << result[i] << ' ';
  std::cout << '\n';

  std::adjacent_difference (val, val+7, result, myop);
  std::cout << "using custom function: ";
  for (int i=0; i<7; i++) std::cout << result[i] << ' ';
  std::cout << '\n';

  // prova con vector
  vector<int> r (7,0);
  vector<int> l( val, val + sizeof(val)/sizeof(val[0]) );

  std::adjacent_difference (l.begin(), l.end(), r.begin());
  std::cout << "using default adjacent_difference on a vector: ";
  for (int i=0; i<7; i++) std::cout << r[i] << ' ';
  std::cout << '\n';


  return 0;
}
